package chrome.entity;

import com.sun.xml.internal.bind.v2.runtime.reflect.opt.Const;
import commons.entity.Constant;
import commons.entity.TrainResults;
import commons.util.GameUtil;
import commons.util.ProcessDealUtil;
import fgoScript.constant.GameConstant;
import fgoScript.entity.PointColor;
import fgoScript.exception.AppNeedRestartException;
import fgoScript.exception.AppNeedStopException;
import fgoScript.exception.AppNeedUpdateException;
import gudaziEx.util.ChainUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.awt.*;
import java.util.List;

public class ChromeService {
    private static final Logger LOGGER = LogManager.getLogger(ChromeService.class);
    public static void GoSignTopToon() throws AppNeedStopException {
        int delayMillions = 1000*Chrome.getInstance().getDelaySeconds();

        List<ChromeAccount> caList = ChromeAccounts.getInstance().getCaList();
        ChromeAccount ca;
        int caListSize = caList.size();
        Chrome.getInstance().startChrome();
        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);
        for (int i = 0; i < caListSize; i++) {
            ca = caList.get(i);
            Chrome.getInstance().startChrome();
            GameUtil.delay(delayMillions);

            GameUtil.getMymouse().mouseMoveByPoint(ca.getHomeClickLoc(),true,true);

            GameUtil.getMymouse().mousePressAndRelease(50);

            GameUtil.delay(delayMillions);
            GameUtil.delay(delayMillions);


            GameUtil.getMymouse().mouseMoveByPoint(ca.getSingnWebAddressClickLoc(),true,true);

            GameUtil.getMymouse().mousePressAndRelease(50);


            try {
                GameUtil.waitUntilOneColor(Chrome.getInstance().getSignatureList(), Constant.CHROMEMonitor);
            } catch (AppNeedUpdateException e) {
                e.printStackTrace();
            } catch (AppNeedRestartException e) {
                e.printStackTrace();
            } catch (AppNeedStopException e) {
                e.printStackTrace();
            }
           goRoll(ca, delayMillions);
            GameUtil.delay(2000);

        }
    }
    public static void goRoll(ChromeAccount ca, int delayMillions) throws AppNeedStopException {
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageSlicePoint(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageSlicePoint(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageSlicePoint(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageRollButton(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


//        GameUtil.delay(delayMillions);
//        GameUtil.img2fileForTopToon(GameConstant.IMG_EXTEND,GameUtil.getPreFix() + "\\Toptoon签到图片_"+i+".");
    }
    public static void GoCheckTopToon() throws AppNeedStopException {
        int delayMillions = 1000*Chrome.getInstance().getDelaySeconds();

        List<ChromeAccount> caList = ChromeAccounts.getInstance().getCaList();
        ChromeAccount ca;
        int caListSize = caList.size();
        Chrome.getInstance().startChrome();
        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        boolean[] results = new boolean[caListSize];

        for (int i = 0; i < caListSize; i++) {
            ca = caList.get(i);
            Chrome.getInstance().startChrome();
            GameUtil.delay(delayMillions);

            GameUtil.getMymouse().mouseMoveByPoint(ca.getHomeClickLoc(),true,true);

            GameUtil.getMymouse().mousePressAndRelease(50);

            GameUtil.delay(delayMillions);
            GameUtil.delay(delayMillions);


            GameUtil.getMymouse().mouseMoveByPoint(ca.getSingnWebAddressClickLoc(),true,true);

            GameUtil.getMymouse().mousePressAndRelease(50);


            try {
                GameUtil.waitUntilOneColor(Chrome.getInstance().getSignatureList(), Constant.CHROMEMonitor);
            } catch (AppNeedUpdateException e) {
                e.printStackTrace();
            } catch (AppNeedRestartException e) {
                e.printStackTrace();
            } catch (AppNeedStopException e) {
                e.printStackTrace();
            }
            results[i] = goCheck(ca, delayMillions);

        }
            TrainResults tr = new TrainResults();
            tr.setToptoon01Finish(results[0]);
            tr.setToptoon02Finish(results[1]);
            tr.setToptoon03Finish(results[2]);
            tr.setToptoon04Finish(results[3]);
            tr.setToptoonSignDate(GameUtil.getNowDateString());
            tr.setGoToptoonReport(true);
            ChainUtil.saveTrainResults(tr);

    }
    public static boolean goCheck(ChromeAccount ca, int delayMillions) throws AppNeedStopException {
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageRollButton(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageRollButton(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);


        GameUtil.getMymouse().mouseMoveByPoint(ca.getSignPageRollButton(),true,true);

        GameUtil.getMymouse().mousePressAndRelease(50);


        GameUtil.delay(delayMillions);
        GameUtil.delay(delayMillions);

       List<PointColor> pcList = Chrome.getInstance().getZeroCheckPoints();
       int size = pcList.size();
       int pointChecks= 0;
       boolean ifFinish = false;
        for (int i = 0; i < size-1; i++) {
           if(GameUtil.likeEqualColor(GameUtil.getScreenPixel(pcList.get(i).getPoint()),GameUtil.getScreenPixel(pcList.get(i+1).getPoint()),5)){
               pointChecks++;
           }
        }
        ifFinish = (pointChecks == size-1) &&
                GameUtil.likeEqualColor(GameUtil.getScreenPixel(Chrome.getInstance().getNeedSignPoint().getPoint()),
                                        Chrome.getInstance().getNeedSignPoint().getColor());
//        System.out.println(ifFinish);
        return ifFinish;

    }

        public static void main(String[] args) {
        try {
            ChromeService.GoCheckTopToon();
        } catch (AppNeedStopException e) {
            e.printStackTrace();
        }
    }
}
